package com.mw.wmimobile.gui.fragments;

import java.util.List;

import android.content.Context;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.mw.wmimobile.commons.Commons;
import com.mw.wmimobile.datastorage.NewsSmallDto;
import com.wm.wmimobile.R;

public class NewsAdapter extends ArrayAdapter<NewsSmallDto> {

	private Context context;
	private List<NewsSmallDto> newsDtoList;

	private TextView nameTextView;
	private TextView descriptionTextView;
	private TextView dateTextView;

	private int[] listItemBackground = new int[] {
			R.drawable.staff_list_backgroung_1_transp,
			R.drawable.staff_list_backgroung_2_transp };

	public NewsAdapter(Context context, List<NewsSmallDto> list) {
		super(context, R.layout.news_adapter_listitem, list);
		this.context = context;
		this.newsDtoList = list;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		View rowView = inflater.inflate(R.layout.news_adapter_listitem, parent,
				false);
		
		int listItemBackgroundPosition = position % listItemBackground.length;
        rowView.setBackgroundResource(listItemBackground[listItemBackgroundPosition]);

		nameTextView = (TextView) rowView
				.findViewById(R.id.FragmentNewsNameTextItem);
		descriptionTextView = (TextView) rowView
				.findViewById(R.id.FragmentNewsDescriptionTextItem);
		dateTextView = (TextView) rowView
				.findViewById(R.id.FragmentNewsDateTextItem);

		NewsSmallDto news = newsDtoList.get(position);
		// nameStr = add.getName() == null ? "null" : add.getName();

		String newsString = news.getName();

		Spanned spanned;
		spanned = android.text.Html.fromHtml("<b>" + newsString + "</b>");
		nameTextView.setText(spanned);

		String published = news.getPublished();

		spanned = android.text.Html.fromHtml(published);
		dateTextView.setText(spanned);

		String desc = news.getSummary();
		desc = Commons.cutName(desc, 60);
		descriptionTextView.setText(desc);

		return rowView;
		
	}

}
